#ifndef BLDCDriver3PWM_h
#define BLDCDriver3PWM_h

#include "../common/base_classes/BLDCDriver.h"
#include "../common/foc_utils.h"
#include "../common/time_utils.h"
#include "../common/defaults.h"
// #include "hardware_api.h"
#include "soc/soc_comm_def.h"
#include "soc/interface/if_soc_timer.h"

/**
 3 pwm bldc driver class
*/
class BLDCDriver3PWM: public BLDCDriver
{
  public:
    /**
      BLDCDriver class constructor
      @param phA A phase pwm pin
      @param phB B phase pwm pin
      @param phC C phase pwm pin
      @param en1 enable pin (optional input)
      @param en2 enable pin (optional input)
      @param en3 enable pin (optional input)
    */
    BLDCDriver3PWM(const GpioPortPin& pinA = GpioPortPin::None(), const GpioPortPin& pinB = GpioPortPin::None(), const GpioPortPin& pinC = GpioPortPin::None(),  
                   const GpioPortPin& en1 = GpioPortPin::None(), const GpioPortPin& en2 = GpioPortPin::None(), const GpioPortPin& en3= GpioPortPin::None());
    
    void attachTimer(IF_SOC_Timer *ta, IF_SOC_Timer *tb, IF_SOC_Timer *tc, int cha, int chb, int chc);

    /**  Motor hardware init function */
  	int init() override;
    /** Motor disable function */
  	void disable() override;
    /** Motor enable function */
    void enable() override;

    // hardware variables
  	GpioPortPin pwmA; //!< phase A pwm pin number
  	GpioPortPin pwmB; //!< phase B pwm pin number
  	GpioPortPin pwmC; //!< phase C pwm pin number
    GpioPortPin enableA_pin; //!< enable pin number
    GpioPortPin enableB_pin; //!< enable pin number
    GpioPortPin enableC_pin; //!< enable pin number
    bool enable_active_high = true;

    IF_SOC_Timer *timerA = nullptr;
    IF_SOC_Timer *timerB = nullptr;
    IF_SOC_Timer *timerC = nullptr;

    int pwmACh = -1;
    int pwmBCh = -1;
    int pwmCCh = -1;

    /** 
     * Set phase voltages to the harware 
     * 
     * @param Ua - phase A voltage
     * @param Ub - phase B voltage
     * @param Uc - phase C voltage
    */
    void setPwm(float Ua, float Ub, float Uc) override;

    /** 
     * Set phase voltages to the harware 
     * 
     * @param sc - phase A state : active / disabled ( high impedance )
     * @param sb - phase B state : active / disabled ( high impedance )
     * @param sa - phase C state : active / disabled ( high impedance )
    */
    virtual void setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) override;
};


#endif
